// pages/travelAgency/addDriver/adddriver.js
var Tool = require("../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IDD: '',
    list: [],
    listSearch: [],
    order_error: false,
    winHeight: 0,
    //弹出框标识
    hiddenmodalput: true,
    //对司机的报价
    price: '',
    partnerID: '',
    tel: '',
  },

  //提示toast
  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },

  callPhone: function (e) {
    if (e.currentTarget.id == "") {
      wx.showToast({
        title: '号码为空',
        icon: 'none',
      });
      return;
    }
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.id //仅为示例，并非真实的电话号码
    });
  },

  pullto: function (e) {
    console.log(e)
    var telNum = e.currentTarget.dataset.telnum
    console.log(telNum)
    this.setData({
      hiddenmodalput: false,//!this.data.hiddenmodalput
      partnerID: e.currentTarget.id,
      tel: telNum
    });
    console.log(this.data.tel)
    
  },

  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },

  confirm: function () {
    pulltoOrder(this)
    this.setData({
      hiddenmodalput: true
    })
  },

  inputchangePrice: function (e) {
    var pri = e.detail.value
    this.setData({
      price: pri
    })
  },
  //悬浮球点击事件
  ballClickEvent: function () {
    wx.navigateTo({
      url: '../travelAgency/addPartnerDriver',
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  inputchange: function (e) {
    var that = this
    var arr = []
    var keyWord = e.detail.value

    for (var i = 0; i < that.data.listSearch.length; i++) {

      if (that.data.listSearch[i].PartnerName.indexOf(keyWord) >= 0 || that.data.listSearch[i].PartnerTel.indexOf(keyWord) >= 0) {
        arr.push(that.data.listSearch[i]);
      }
    }
    if (keyWord != null && keyWord != undefined && keyWord != '' && keyWord != ' ' && keyWord.length != 0) {
      that.setData({
        list: arr,
      });
    } else {
      that.setData({
        list: that.data.listSearch
      });
    }
    console.log('1')
    console.log(that.data.list)
  },


  delete: function (e) {
    var that = this
    wx: wx.showModal({
      title: '确定删除?',
      content: '确定删除这个合作司机吗?',
      showCancel: true,
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          var partnerID = e.currentTarget.id
          deletePartner(that, partnerID)
        } else {

        }
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  //新增司机推送方法
  PushToDriver: function () {
    var that = this
console.log(that.data.tel)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/Weixin/WxPushHdl.ashx',
      data: {
        idd: that.data.IDD,
        tel: that.data.tel,
        title: '您有新的订单来了',
        content: '旅行社给您发送了一个新的订单'
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //隐藏Toast
          wx.hideToast();
          //显示错误信息
          that.showError('接口请求失败，请重试')
          return
        }
        // success
        // var data = tool.getRegularJson(res.data);
        var code = res.data.code;
        console.log(res);
        if (code == 0) {
          console.log('post推送请求成功')

        } else {
          console.log(res)
        }
      },
      fail: function (res) {
        console.log(res)
      },
      complete: function () {
        // complete
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    var IDD1 = options.IDD;
    this.setData({
      IDD: IDD1,

    });
    if (!IDD1) {
      this.setData({
        order_error: true
      });
    } else {


    }

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winHeight: res.windowHeight
        });
      }
    });
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getServiceData(this)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },



  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


})

function getServiceData(that) {
  that.data.list = []
  that.data.listSearch = []
  wx.showToast({
    title: '正在加载...',
    icon: 'loading',
    duration: 10000
  });
  var userInfo = wx.getStorageSync('userInfo');
  console.log(userInfo)
  var useid = userInfo.ID
  wx.request({
    url: 'https://www.lanshuicar.com/Server/CooPeratePartners.ashx',
    data: {
      PageSize: '200',
      PageIndex: '1',
      ID: useid,
      CType: '3',
      Info: '0',
      Type: '1'

    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      if (res.statusCode == 404) {
        //接口加载失败
        console.log('加载接口错误');
        //隐藏Toast
        wx.hideToast();
        //显示错误信息
        that.showError('接口请求失败，请重试')
        return
      }
      // success

      var data = tool.getRegularJson(res.data);
      var code = data.code;
      console.log(res);
      if (code == 0) {
        that.setData({
          list: that.data.list.concat(data.data),
          listSearch: that.data.listSearch.concat(data.data)
        });
      } else {
        that.setData({
          order_error: true
        });

      }
    },
    fail: function () {
      // fail
      that.setData({
        order_error: true
      });
    },
    complete: function () {
      // complete
      wx.hideToast();
      wx.stopPullDownRefresh();
    }
  })
}

function pulltoOrder(that) {
  var userInfo = wx.getStorageSync('userInfo');
  wx.showToast({
    title: '正在加载...',
    icon: 'loading',
    duration: 10000
  });
  wx.request({

    url: 'https://www.lanshuicar.com/Server/AgencyServer/AgencyAddCar.ashx',
    data: {
      IDD: that.data.IDD,
      methos: 'add',
      Tel: userInfo.Telphone,
      GID: that.data.partnerID,
      Price: that.data.price
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      if (res.statusCode == 404) {
        //接口加载失败
        console.log('加载接口错误');
        //隐藏Toast
        wx.hideToast();
        //显示错误信息
        that.showError('接口请求失败，请重试')
        return
      }
      // success
      var data = tool.getRegularJson(res.data);
      var code = data.code;
      console.log(res);
      if (code == 0) {
        wx.showToast({
          title: '拉入订单成功',
        });
        that.PushToDriver()
        choosecomplete();
        // getServiceData(thiss)
      } else {
        wx.showToast({
          title: '添加失败，' + data.msg + '!',
          icon: 'none',
          duration: 3000
        })
      }
    },
    fail: function () {

    },
    complete: function () {
      // complete
      // wx.hideToast();
      // wx.stopPullDownRefresh();
    }
  })
}

function choosecomplete() {
  var pages = getCurrentPages();
  // var currpage = pages[pages.length - 1];
  // var prevpage = pages[pages.length - 2];
  wx.navigateBack();
}

function deletePartner(that, PartnerID) {
  var userInfo = wx.getStorageSync('userInfo')
  wx.request({
    url: 'https://www.lanshuicar.com/Server/AgencyServer/AgencyAddCoopPartners.ashx',
    data: {
      ID: userInfo.ID,
      PID: PartnerID,
      Type: 1,
      OpType: 1,
    },
    method: 'GET',
    success: function (res) {
      if (res.statusCode == 404) {
        //接口加载失败
        console.log('加载接口错误');
        //隐藏Toast
        wx.hideToast();
        //显示错误信息
        that.showError('接口请求失败，请重试')
        return
      }
      // success
      var code = res.data.code;
      if (code == 0) {
        wx.showToast({
          title: '添加成功',
        })
        getServiceData(that)
      }
    },
    fail: function () {
      // fail
    }
  })
}